package com.prashantdctm.utils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Map.Entry;

import org.apache.log4j.Logger;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;

public class GaeUtil {
	private static final Logger log = Logger
			.getLogger(GaeUtil.class.getName());

	public static void main(String[] args) {
		sendReport();

	}

	public static void saveResults(String site, String user, int messagesRead,
			String earningsAfter, String timeTaken) {
		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();
		String guestbookName = "PaidEmail";
		Key guestbookKey = KeyFactory.createKey("PaidEmail", guestbookName);

		log.info("Saving results");
		Date date = new Date();
		Entity messageResults = new Entity("PaidEmail", guestbookKey);
		messageResults.setProperty("date", date);
		messageResults.setProperty("site", site);
		messageResults.setProperty("user", user);
		messageResults.setProperty("messagesread", messagesRead);
		messageResults.setProperty("earningsafter", earningsAfter);
		messageResults.setProperty("timetaken", timeTaken);
		datastore.put(messageResults);
	}

	public static void sendReport() {
		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();
		String guestbookName = "PaidEmail";
		Key guestbookKey = KeyFactory.createKey("PaidEmail", guestbookName);

		// The Query interface assembles a query
		Query q = new Query("Greeting");
		//q.addFilter("date", Query.FilterOperator.LESS_THAN, new Date());
		
		// PreparedQuery contains the methods for fetching query results
		// from the datastore
		PreparedQuery pq = datastore.prepare(q);

		for (Entity result : pq.asIterable()) {
		  Date date = (Date) result.getProperty("date");
		  String earningsAfter = (String) result.getProperty("earningsafter");
		  String messagesRead = (String) result.getProperty("messagesread");
		  String site = (String) result.getProperty("site");
		  String timetaken = (String) result.getProperty("timetaken");
		  String user = (String) result.getProperty("user");
		  
		  System.out.println(date + " " + earningsAfter + ", " + messagesRead + " ");
		}
		
		
	}

}
